package com.canyou.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.canyou.constants.CommonConstants;
import com.canyou.datascope.DataScope;
import com.canyou.enums.DataPermEnum;
import com.canyou.system.entity.dept.DeptListVO;
import com.canyou.system.entity.dept.DeptVO;
import com.canyou.system.mapper.DeptMapper;
import com.canyou.system.model.Dept;
import com.canyou.system.service.DeptService;
import com.canyou.system.service.UserService;
import com.canyou.utils.AbstractTreeNode;
import com.canyou.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门服务实现类
 *
 * @author canyou
 * @since 2019-05-14
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private UserService userService;

    @Override
    public List<DeptVO> getDeptTree(String deptName, String orgName) {
        List<DeptVO> deptList = baseMapper.getNoPageList(new DataScope().setDataPermEnum(DataPermEnum.ORG_ONLY), deptName, orgName);
        List<DeptVO> treeList = TreeUtils.build(deptList, CommonConstants.DEFAULT_PARENT_ID);
        return treeList;
    }

    @Override
    public DeptVO getDeptDetailById(Long id) {
        return baseMapper.getDeptDetailById(id);
    }

    @Override
    public boolean existDept(Long orgId, String deptName, Long deptId) {
        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getOrgId, orgId)
                .eq(Dept::getDeptName, deptName);
        if (deptId != null) {
            queryWrapper.ne(Dept::getId, deptId);
        }
        return baseMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public int getDeptChildCount(Long id) {
        return baseMapper.getDeptChildCount(id);
    }

    @Override
    public long getDeptUserCount(Long deptId) {
        return userService.getUserCountByDeptId(deptId);
    }

    @Override
    public boolean removeById(Long roleId) {
        return baseMapper.deleteById(roleId) > 0;
    }

    @Override
    public List<DeptListVO> getDeptAndChildList(Long rootId) {
        List<DeptListVO> list = baseMapper.getAllDeptListVO();
        List<DeptListVO> newList = new ArrayList<>();
        // 将自己本身加入到集合中
        if (!rootId.equals(CommonConstants.DEFAULT_PARENT_ID)) {
            newList.addAll(list.stream()
                    .filter(org -> rootId.equals(org.getId()))
                    .collect(Collectors.toList()));
        }
        TreeUtils.buildList(list, newList, rootId);
        return newList;
    }

    @Override
    public List<Long> getDeptAndChildIdList(Long rootId) {
        return getDeptAndChildList(rootId).stream()
                .map(AbstractTreeNode::getId)
                .collect(Collectors.toList());
    }

}
